﻿Imports System.Reflection
Imports System.IO
Imports System


Module Module1

    ' Trying different methods of creating an object without a dependency showing
    '    obj = Assembly.CreateInstance("typeName")
    '    obj = System.Activator.CreateInstance("typeName","URIToAssembly")
    Sub Main()
        Dim assem As [Assembly] = [Assembly].GetExecutingAssembly()

        Dim typeName As String = "CILADA.TestAssemblyFive.VBHello"
        Dim obj As Object = assem.CreateInstance(typeName)
        WriteInfo(obj)

        Console.WriteLine("#################")

        Dim helloType As System.Type = System.Type.GetType("CILADA.TestAssemblyFive.VBHello")
        Dim obj2 As Object = Activator.CreateInstance(helloType)
        WriteInfo(obj2)

        Console.WriteLine("#################")

        Dim assemblyPath As String = "C:\Documents and Settings\andrew.esler\My Documents\Projects\CILADA\CILADA.Test\TestAssemblies\CILADA.TestAssemblyFive\bin\Debug\CILADA.TestAssemblyFour.exe"

        Dim assemName As FileInfo = New FileInfo("CILADA.TestAssemblyFour.exe")
        Console.WriteLine("Assembly=" + assemName.FullName)
        Dim obj3 As Object = System.Activator.CreateInstanceFrom(assemblyPath, "CILADA.TestAssemblyFour.Hello")
        'Dim handle As System.Runtime.Remoting.ObjectHandle = CType(obj2, System.Runtime.Remoting.ObjectHandle)
        'obj3 = handle.Unwrap()
        WriteInfo(obj3)

        Console.WriteLine("#################")

        Dim type As Type = Assembly.LoadFile(assemblyPath).GetType("CILADA.TestAssemblyFour.Hello")
        Dim obj4 As Object = System.Activator.CreateInstance(type)
        WriteInfo(obj4)

        Console.ReadKey(True)

    End Sub

    Sub WriteInfo(ByRef obj As Object)
        Console.WriteLine("Created object from assembly {0}, of type {1}", obj.GetType().Assembly, obj.GetType().FullName)
    End Sub

End Module
